package lovely.gwt.jsonrpc.rebind.post;

import rocket.generator.rebind.codeblock.TemplatedFileCodeBlock;
import rocket.generator.rebind.type.Type;
import rocket.util.client.Checker;

public class WriteObjectTemplatedFile extends TemplatedFileCodeBlock {

	/**
	 * THe parameter type of the only parameter
	 */
	private Type parameterType;

	protected Type getParameterType() {
		Checker.notNull("field:parameterType", parameterType);
		return this.parameterType;
	}

	public void setParameterType(final Type parameterType) {
		Checker.notNull("parameter:parameterType", parameterType);
		this.parameterType = parameterType;
	}

	/**
	 * The key of the template file
	 */
	private String template;

	public void setTemplate(final String template) {
		Checker.notNull("parameter:template", template);
		//TODO write test
		if (!WriteObjectConstants.TEMPLATE_NAMES.containsKey(template)) {
			Checker.fail("parameter:template", "unknown key");
		}
		this.template = template;
	}

	@Override
	protected String getResourceName() {
		Checker.notNull("field:template", template);
		return WriteObjectConstants.TEMPLATE_NAMES.get(template);
	}

	@Override
	protected Object getValue0(String name) {
		Object value = null;
		while (true) {
			if (WriteObjectConstants.PARAMETER_TYPE.equals(name)) {
				value = this.getParameterType();
				break;
			}
			break;
		}
		return value;
	}

}
